package cn.stylefeng.guns.core.util;



import java.util.*;

public class WeixinConfig {
	/**
	 * API的密钥
	 */
	public static final String API_KEY="baize41ufzOmhP329xAI4kZX87Mht0d3";

	private  static String access_token;
	private  static String jsapi_ticket;
	private  static Date access_tokenCreatTime;
	private  static Date jsapi_ticketCreatTime;
	public static String getAccess_token() {
		return access_token;
	}
	public static void setAccess_token(String access_token) {
		WeixinConfig.access_token = access_token;
	}
	public static String getJsapi_ticket() {
		return jsapi_ticket;
	}
	public static void setJsapi_ticket(String jsapi_ticket) {
		WeixinConfig.jsapi_ticket = jsapi_ticket;
	}
	public static Date getAccess_tokenCreatTime() {
		return access_tokenCreatTime;
	}
	public static void setAccess_tokenCreatTime(Date access_tokenCreatTime) {
		WeixinConfig.access_tokenCreatTime = access_tokenCreatTime;
	}
	public static Date getJsapi_ticketCreatTime() {
		return jsapi_ticketCreatTime;
	}
	public static void setJsapi_ticketCreatTime(Date jsapi_ticketCreatTime) {
		WeixinConfig.jsapi_ticketCreatTime = jsapi_ticketCreatTime;
	}


    /**
     * 验证回调签名
     * @return
     */
    public static boolean isTenpaySign(Map<String, String> map) {
    	String characterEncoding="utf-8";
       String charset = "utf-8";
       String signFromAPIResponse = map.get("sign");
       if (signFromAPIResponse == null || signFromAPIResponse.equals("")) {
    	   System.out.println("API返回的数据签名数据不存在，有可能被第三方篡改!!!"); 
    	   return false;
       }
       System.out.println("服务器回包里面的签名是:" + signFromAPIResponse);
     //过滤空 设置 TreeMap
       SortedMap<String,String> packageParams = new TreeMap();
       
       for (String parameter : map.keySet()) {
    	   String parameterValue = map.get(parameter);
    	   String v = "";
    	   if (null != parameterValue) {
    		   v = parameterValue.trim();
    	   }
    	   packageParams.put(parameter, v);
       }
       
       StringBuffer sb = new StringBuffer();
       Set es = packageParams.entrySet();
       Iterator it = es.iterator();
       
       while(it.hasNext()) {
    	   Map.Entry entry = (Map.Entry)it.next();
    	   String k = (String)entry.getKey();
    	   String v = (String)entry.getValue();
    	   if(!"sign".equals(k) && null != v && !"".equals(v)) {
    		   sb.append(k + "=" + v + "&");
    	   }
       }
       sb.append("key=" + API_KEY);
       
     //将API返回的数据根据用签名算法进行计算新的签名，用来跟API返回的签名进行比较
     //算出签名
       String resultSign = "";
       String tobesign = sb.toString();
       
       if (null == charset || "".equals(charset)) {
    	   resultSign = MD5Util.MD5Encode(tobesign, characterEncoding).toUpperCase();
       }else{
    	   try{
    		   resultSign = MD5Util.MD5Encode(tobesign, characterEncoding).toUpperCase();
    	   }catch (Exception e) {
    		   resultSign = MD5Util.MD5Encode(tobesign, characterEncoding).toUpperCase();
    	   }
       }
       
       String tenpaySign = ((String)packageParams.get("sign")).toUpperCase();
       return tenpaySign.equals(resultSign);
    }

	 public static String GetMapToXML(Map<String,String> param){  
        StringBuffer sb = new StringBuffer();  
        sb.append("<xml>");  
        for (Map.Entry<String,String> entry : param.entrySet()) {   
               sb.append("<"+ entry.getKey() +">");  
               sb.append(entry.getValue());  
               sb.append("</"+ entry.getKey() +">");  
       }    
        sb.append("</xml>");  
        return sb.toString();  
    } 

}
